﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Capstone.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CheckboxPage : BasePage<string>
    {
        private List<CheckedItem> ItemsSource;
        public static List<string> items;
        public CheckboxPage(List<string> itemsSource, List<int> selectedIndices)
        {
            InitializeComponent();
            items = itemsSource;
            List<CheckedItem> list = new List<CheckedItem>();
            for (int i = 0; i < itemsSource.Count; i++)
            {
                list.Add(new CheckedItem()
                {
                    Text = itemsSource[i],
                    IsChecked = false,
                    Position = i
                });
            }
            ItemsSource = list;
            foreach (int i in selectedIndices)
            {
                list[i].IsChecked = true;
            }
            listView.ItemsSource = list;

            if (items[0] == Properties.Resources.StoolType1)
            {
                add.IsVisible = false;
            }

            

        }

        private void listView_ItemTapped(object sender, ItemTappedEventArgs e)
        {

            //CheckedItem ChBox = (CheckedItem)listView.SelectedItem;
            //if (ChBox.IsChecked)
            //{
            //    ChBox.IsChecked = false;
                
            //}
            //else
            //{
            //    ChBox.IsChecked = true;
            //}


        }

        private void Add_Clicked (object sender, EventArgs e)
        {
            Shell.Current.GoToAsync(nameof(NewSymptomPage));
        }

        private void Done_Clicked(object sender, EventArgs e)
        {
            int checkedItems = 0;
            foreach (CheckedItem item in ItemsSource)
            {
                if (item.IsChecked)
                {
                    checkedItems++;
                }
            }
            if (items[0] == Properties.Resources.GutSymptom1 && checkedItems > 10)
            {
                gutError.IsVisible = true;
            }
            else if (items[0] == Properties.Resources.OtherSymptom1 && checkedItems > 10)
            {
                otherError.IsVisible = true;
            }
            else if (items[0] == Properties.Resources.SleepSymptom1 && checkedItems > 5)
            {
                sleepError.IsVisible = true;
            }
            else if (items[0] == Properties.Resources.StoolQuestions1 && checkedItems > 10)
            {
                questionError.IsVisible = true;
            }
            else
            {
                gutError.IsVisible = false;
                otherError.IsVisible = false;
                sleepError.IsVisible = false;
                questionError.IsVisible = false;
                List<CheckedItem> list = (List<CheckedItem>)listView.ItemsSource;
                _navigationResut = string.Join(",", list.Where(x => x.IsChecked).Select(x => x.Position).ToArray());
                Navigation.PopModalAsync();
            }
            
        }


        private void Cancel_Clicked(object sender, EventArgs e)
        {
            //List<CheckedItem> list = ItemsSource;
            _navigationResut = "";// string.Join(",", list.Where(x => x.IsChecked).Select(x => x.Position).ToArray());
            Navigation.PopModalAsync();
        }
    }

    public class CheckedItem
    {
        public int Position { get; set; }
        public bool IsChecked { get; set; }
        public string Text { get; set; }
    }
}